// pages/ownerOertification/ownerOertification.js
var app = getApp();
var util = require('../../utils/util.js');
var applet_park_api_url = app.globalData.applet_park_api_url;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    responsiblePersonId: '',
    responsiblePersonName: '',
    responsiblePersonIdCardNo: '',
    responsiblePersonPhone: '',
    businessScope: '',
    buildingId: '',
    roomId: '',
    profile: '',
    businessLicenseNo: '',
    businessLicenseImage: '',
    roomNumber: '',
    houseArea: '',
    listarea: ['请选择', 'A区', 'B区', 'C区', 'D区'],
    listareaid: [null, 'personal_use', 'business_use', 'b2p_use', 'rental_use'],
    listareaindex: 0,
    listfloor: [],
    listfloorid: [],
    listfloorindex: 0,
    Positive: '',
    unPositive: '',
    detailInfo: {
      pic1: null,
      pic2: null,
    },
    areaId: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.loadAreas();
  },
  //选择
  changeUsedType: function(e) {
    
    this.data.areaId = this.data.listareaid[e.detail.value];
    this.data.listareaindex = e.detail.value;
    this.setData(this.data);
    this.loadFloor();
  },
  changefloor: function(e) {
    
    if (!this.data.areaId) {
      util.showModal('请先选择楼栋');
      return
    }
    
    this.data.floorId = this.data.listfloorid[e.detail.value];
    this.data.listfloorindex = e.detail.value;
    this.setData(this.data);

  },
  bindKeyInput: function(e) {
    this.data[e.currentTarget.dataset.inputname] = e.detail.value;
    this.setData(this.data);
  },
  //加载区域
  loadAreas() {
    var that = this;
    wx.request({
      url: applet_park_api_url + '/api/buildings',
      success(res) {
        
        let listarea = [];
        listarea.push("请选择楼栋")
        let listareaid = [];
        listareaid.push("null")
        if (res.data.code == 0) {
          for (let item of res.data.data) {
            
            listarea.push(item.name)
            listareaid.push(item.id)
          }
          

          that.data.listarea = listarea;
          that.data.listareaid = listareaid;
          that.setData(that.data);
        } else {
          wx.showToast({
            title: '网络异常',
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: res.data,
        })
      }
    })
  },
  loadFloor() {
    var that = this;
    wx.request({
      url: applet_park_api_url + '/api/rooms?buildingId=' + that.data.areaId,
      success(res) {
        
        let listfloor = [];
        listfloor.push("请选择房间号")
        let listfloorid = [];
        listfloorid.push("null")
        if (res.data.code == 0) {
          for (let item of res.data.data) {
            
            listfloor.push(item.name)
            listfloorid.push(item.id)
          }
          

          that.data.listfloor = listfloor;
          that.data.listfloorid = listfloorid;
          that.setData(that.data);
        } else {
          wx.showToast({
            title: res.data.mesage,
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: res.data.message
        })
      }
    })
  },
  chooseImage: function(e) {

    var that = this;
    let pic = e.currentTarget.dataset.pic;
    
    wx.chooseImage({
      count: 1,
      success: function(res) {
        that.data.detailInfo[pic] = res.tempFiles[0];

        that.setData({
          detailInfo: that.data.detailInfo
        });
        
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  upload: function() {
    var that = this;
    wx.showLoading({
      title: '图片保存中',
      mask: true
    });

    var finished = true;
    for (var j = 1; j <= 18; j++) {
      var pic = that.data.detailInfo['pic' + j];
      if (pic && pic.path && !pic.picUrl) { //false
        finished = false;
        break;
      }
    }
    if (finished) {
      wx.hideLoading();
      // if (that.data.detailInfo.pic14) {
      //   that.btn();
      // }
      // // else {
      // //   that.submit();
      // // }
      that.submit();
      return;
    }
    //用自执行函数,来解决循环内部的异步回调问题
    (function uploadImage(i) {
      if (i > 5) {
        return;
      }
      
      // var image = that.data.imageList[i];
      if (that.data.detailInfo['pic' + i] && that.data.detailInfo['pic' + i].path && !that.data.detailInfo['pic' + i].picUrl) {
        var path = that.data.detailInfo['pic' + i].path;
        wx.uploadFile({
          url: 'https://jinchengit.cn:9999/api/upload',
          filePath: path,
          name: 'files',
          success: function(res) {
            const response = JSON.parse(res.data)
            if (response.code == 0) {
              that.data.detailInfo['pic' + i].picUrl = "http://zjurainforest.com:9999" + response.data[0];
              that.setData(that.data)
              
            } else {
              wx.showToast({
                title: '图片上传失败',
              })
              wx.hideLoading();
            }
            
            // that.data.detailInfo['pic' + i].picUrl = "/" + res;
            var allFinished = true;
            for (var j = 1; j <= 4; j++) {
              var pic = that.data.detailInfo['pic' + j];
              if (pic && pic.path && !pic.picUrl) { //false
                allFinished = false;
                break;
              }
            }
            if (allFinished) { //全部传完了,则隐藏加载按钮并返回
              wx.hideLoading();
              that.submit();
            } else {
              uploadImage(i + 1);
            }
          },
          fail(res) {
            wx.hideLoading();
            util.showModal('网络异常,请稍后重试');

          }
        })
      } else {
        uploadImage(i + 1);
      }
    })(0);
  },
  submit() {
    let that = this;
    var id = wx.getStorageSync("id");
    wx.request({
      url: applet_park_api_url + '/api/shops/applyCertificate',
      method: 'PUT',
      data: {
        name: that.data.name,
        responsiblePensonId: id,
        responsiblePersonName: that.data.responsiblePersonName,
        responsiblePersonIdCardNo: that.data.responsiblePersonIdCardNo,
        responsiblePersonPhone: that.data.responsiblePersonPhone,
        businessScope: that.data.businessScope,
        buildingId: that.data.areaId,
        roomId: that.data.floorId,
        profile: that.data.profile,
        // businessLicenseNo: that.data.detailInfo.pic1.picUrl,
        // businessLicenseImage: that.data.detailInfo.pic2.picUrl,
      },
      success(res) {
        if (res.data.code == 0) {
          wx.showToast({
            title: '提交成功',
          })
        } else {
          wx.showToast({
            title: '提交失败',
          })
        }
      },
      fail() {

      }
    })
  }

})